import { Injectable } from '@angular/core'
import { HttpClientService } from 'src/app/common/httpClient/http'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { CoolLocalStorage } from 'src/app/common/coolStorage/cool-local-storage'

@Injectable({
  providedIn: 'root'
})
export class ActivityService {
  project = ''
  start = ''
  end = ''
  zone = ''
  entityStart = ''
  entityEnd = ''
  reqdata = {
    pager: { Start: 0, Limit: 10 },
    filter: {
      Lambda: '',
      Navigations: []
    },
    sorters: [{ Property: 'item=>item.UpdateTime', Direction: 'Descending' }]
  }

  constructor(private http: HttpClientService, private utils: CommonUtil, private localStorage: CoolLocalStorage) {
    this.start = this.utils.time2UTC(new Date())
    this.end = this.utils.time2UTC(new Date())
    this.entityStart = this.utils.time2UTC(new Date())
    this.entityEnd = this.utils.time2UTC(new Date())
    this.zone = this.localStorage.getItem('zone')
  }

  getList(Start, callback, errorBack?) {
    let str = `item.Project==${this.project}&&`
    let lambda = `item=>${this.project ? str : ''}item.Start<=${this.start}&&item.End>=${
      this.end
    }&&item.ProjectInfo.Cooperation.Any(entity=>!entity.Enable && entity.Publish&&entity.Zone==${
      this.zone
    }&&(entity.Start<=${this.entityStart}&&entity.End>=${this.entityEnd}))`
    this.reqdata.pager.Start = Start
    this.reqdata.filter.Lambda = lambda
    this.http.post(this.reqdata, '/House1/Json/PromotionService.svc/QueryPromotionSummary', callback, errorBack)
  }
  getDetail(promotion, callback, errorBack?) {
    let reqdata = { promotion: promotion, zone: this.localStorage.getItem('zone') }
    this.http.post(reqdata, '/House1/Json/PromotionService.svc/Fetch', callback, errorBack)
  }
  toggleFavorite(reqdata, callback, errorBack?) {
    this.http.post(reqdata.body, reqdata.url, callback, errorBack)
  }
}
